var RankList = require('Rank').RankList;
var createImage = require('Image').createImage;

//个人 openId
wx.wx2g_openid = "";
//群排行
wx.wx2g_groupRanks = null;
//排行榜
wx.wx2g_ranks = null;
//图片缓存
wx.images = {};
//显示的排行榜信息
wx.wx2g_ranksShow = {};
//显示的荣誉殿堂信息
wx.wx2g_honorsShow = {};

wx.onMessage(data => {
    //console.log("[开放域] " +  data.command);
    if(wx.wx2g_openid == "" && data.openid ) wx.wx2g_openid = data.openid;
    if(data.command == "loadImageCache") loadImageCache();
    if(data.command == "initRankList") initRankList(data);
    if(data.command == "uploadRank") uploadRank(data);
    if(data.command == "resetRankPage") resetRankPage(data);
    if(data.command == "drawRank") drawRank(data);
    if(data.command == "rankPageRight") rankPageRight(data);
    if(data.command == "rankPageLeft") rankPageLeft(data);
    if(data.command == "drawBestScore") drawBestScore(data);
    if(data.command == "drawRankIndex") drawRankIndex(data);
    if(data.command == "drawNextRank") drawNextRank(data);
    if(data.command == "drawMiniRank") drawMiniRank(data);
    if(data.command == "drawNextUser") drawNextUser(data);
    if(data.command == "drawHead") drawHead(data);
    if(data.command == "drawShowRank") drawShowRank(data);
    if(data.command == "drawMiniHonor") drawMiniHonor(data);
    if(data.command == "resetHonorPage") resetHonorPage(data);
    if(data.command == "drawHonor") drawHonor(data);
    if(data.command == "honorPageRight") honorPageRight(data);
    if(data.command == "honorPageLeft") honorPageLeft(data);
    if(data.command == "drawOverStep") drawOverStep(data);
    if(data.command == "getRankGroupList") getRankGroupList(data);
});

//加载图片缓存
function loadImageCache(datas) {
    // createImage("res/rank/rankItemBg1.png");
    // createImage("res/rank/rankItemBg2.png");
    // createImage("res/rank/rectYellow.png");
    createImage("res/rank/rankBg1.png");
    createImage("res/rank/rankBg2.png");
    createImage("res/rank/rankBg3.png");
    createImage("res/rank/rankBg4.png");
    // createImage("res/rank/nextRankBg.png");
    // createImage("res/rank/nextRankBg2.png");
    // createImage("res/rank/1.png");
    // createImage("res/rank/2.png");
    // createImage("res/rank/3.png");
    // createImage("res/rank/mask.png");
    // createImage("res/show/1.png");
    // createImage("res/show/2.png");
    // createImage("res/show/3.png");
    // createImage("res/show/4.png");
    // createImage("res/show/5.png");
    // createImage("res/show/honorbg1.png");
    // createImage("res/show/honorbg2.png");
    // createImage("res/show/honorbg3.png");
    // createImage("res/show/honorbg4.png");
    // createImage("res/show/honorbg5.png");
    // createImage("res/show/honorbg1_1.png");
    // createImage("res/show/honorbg1_2.png");
    // createImage("res/show/honorbg1_3.png");
    // createImage("res/show/honorbg1_4.png");
    // createImage("res/show/honorbg1_5.png");
}

/**
 * 获取群排名
 * @param data
 */
function  getRankGroupList(data) {
    wx.wx2g_groupRanks = new RankList(wx.wx2g_openid,data.names, data.shareTicket);
}
//加载排行榜
function initRankList(data) {
    wx.wx2g_ranks = new RankList(wx.wx2g_openid,data.names);
}

//上传排行榜分数
function uploadRank(data) {
    let name = data.name;
    let score = data.score;
    let myRankIndex = wx.wx2g_ranks.getRankIndex(name,wx.wx2g_openid);
    let myRank = wx.wx2g_ranks.getAt(name,myRankIndex);
    if(score > myRank.value) {
        myRank.value = score;
        wx.setUserCloudStorage({
            KVDataList: [ {key:name,value:"" + score}],
        });
    }
}

//重置排行榜页数
function resetRankPage(data) {
    wx.wx2g_ranksShow[data.name] = {page:0,pageCount:data.pageCount};
}

//排行榜向左翻页
function rankPageLeft(data) {
    if(wx.wx2g_ranksShow[data.name].page > 0) wx.wx2g_ranksShow[data.name].page--;
}

//排行榜向右翻页
function rankPageRight(data) {
    if(wx.wx2g_ranksShow[data.name].page < Math.ceil(wx.wx2g_ranks.getLength(data.name)/wx.wx2g_ranksShow[data.name].pageCount) - 1 )
        wx.wx2g_ranksShow[data.name].page++;
}

//绘制排行榜
function drawRank(data) {
    let name = data.name; //排行榜名称
    let style = data.style; //排行榜样
    updateRankShow(name,style,wx.wx2g_ranksShow[name].page,wx.wx2g_ranksShow[name].pageCount);
}

function updateRankShow(name,style,page,pageCount) {
    let sharedCanvas = wx.getSharedCanvas();
    let ctx = sharedCanvas.getContext('2d');
    ctx.clearRect(0,0,sharedCanvas.width,sharedCanvas.height);
    let ranks = wx.wx2g_ranks;
    if(ranks == null || ranks.ranks == null || ranks.ranks.rank == null)return;
    ranks.sort(name);
    if(style == 1) {
        ctx.textBaseline = "alphabetic";
        let len = ranks.getLength(name);
        for(let i = 0; i < pageCount && i + page * pageCount < len; i++) {
            let index = i + page * pageCount;
            let rank = ranks.getAt(name,index);
            let user = rank.user;
            //绘制名次
            //如果是前三名有特殊的图标
            if(index < 3) ctx.drawImage(createImage("res/rank/" + (i + 1) + ".png"), 20, 3 + 115 * i, 74, 75);
            ctx.textAlign = "center";
            ctx.font = "30px '微软雅黑'";
            if(index < 3) ctx.fillStyle = 'rgb(16, 33, 63)';
            else ctx.fillStyle = 'rgb(255, 251, 235)';
            ctx.fillText(index + 1, 58, 40 + 115 * i);
            //绘制头像
            ctx.drawImage(user.img, 95, 0 + 115 * i, 65, 65);
            //名称
            ctx.textAlign = "left";
            ctx.font = "24px '微软雅黑'";
            ctx.fillStyle = 'rgb(255, 251, 235)';
            ctx.fillText(user.name, 185, 60 + 115 * i);
            //分数
            ctx.fillText(rank.value, 425, 60 + 115 * i);
            console.log("排名" + i);
            console.log(rank);
        }
        //绘制页面数字
        ctx.textAlign = "center";
        ctx.font = "28px '微软雅黑'";
        ctx.fillStyle = 'rgb(0, 0, 0)';
        ctx.fillText((page + 1) + "/" + (Math.ceil(len/pageCount) || 1), 275, 45 + 115 * 5);
        //绘制自己的名次
        let myRankIndex = ranks.getRankIndex(name,wx.wx2g_openid);
        if(myRankIndex >= 0) {
            let rank = ranks.getAt(name,myRankIndex);
            let user = rank.user;
            //绘制名次
            ctx.font = "22px '微软雅黑'";
            ctx.fillText(myRankIndex + 1, 58, 40 + 712);
            //绘制头像
            ctx.drawImage(user.img, 105, 0 + 712, 60, 60);
            //名称
            ctx.textAlign = "left";
            ctx.font = "24px '微软雅黑'";
            ctx.fillStyle = 'rgb(50, 35, 13)';
            ctx.fillText(user.name, 185, 60 + 712);
            //分数
            ctx.fillText(rank.value, 425, 60 + 712);
        }
    }else if(style == 2) {
        ctx.textBaseline = "alphabetic";
        let len = ranks.getLength(name);
        for(let i = 0; i < pageCount && i + page * pageCount < len; i++) {
            let index = i + page * pageCount;
            let rank = ranks.getAt(name,index);
            let user = rank.user;
            //绘制名次
            //如果是前三名有特殊的图标
            ctx.textAlign = "center";
            ctx.font = "40px '微软雅黑'";
            ctx.fillStyle = 'rgb(255, 247, 237)';
            ctx.fillText(index + 1, 290, 100 + 135 * i);
            if (index < 3) ctx.drawImage(createImage("res/rank/rankBg" + (i + 1) + ".png"), 270, 55 + 135 * i, 53, 69);
            //绘制头像
            ctx.drawImage(user.img, 170,70 + 136.5 * i, 58, 58);
            //ctx.drawImage(createImage("res/rank/rankBg4.png"), 150, 165+ 100 * i, 71, 71);
            //名称
            ctx.textAlign = "left";
            ctx.font = "22px '微软雅黑'";
            ctx.fillStyle = 'rgb(255, 247, 237)';
            ctx.fillText(user.getNameLength(11), 330, 95 + 135 * i);
            //称号
            //ctx.drawImage(createImage("res/rank/rectYellow.png"), 120, 70 + 30 + 92 * i, 140, 40);
            //分数
			ctx.textAlign = "right";
            ctx.font = "25px '微软雅黑'";
			ctx.fillStyle = 'rgb(255, 247, 237)';
            ctx.fillText(rank.value + "光年", 480, 140 + 135 * i);
            //console.log("排名" + i);
            console.log(rank);
        }
        //绘制页面数字
		ctx.textAlign = "center";
		ctx.font = "26px '微软雅黑'";
		ctx.fillStyle = 'rgb(255, 247, 237)';
		ctx.fillText((page + 1) + "/" + (Math.ceil(len/pageCount) || 1), 340, 65 + 135 * 5);
     
    }
}

//绘制最高分数
function drawBestScore(data) {
    let sharedCanvas = wx.getSharedCanvas();
    let ctx = sharedCanvas.getContext('2d');
    ctx.clearRect(0,0,sharedCanvas.width,sharedCanvas.height);
    let name = data.name; //排行榜名称
    let color = data.color;
    let size = data.size;
    let font = data.font;
    let ranks = wx.wx2g_ranks;
    if(!ranks.getLength(name)) return;
    ranks.sort(name);
    let myRankIndex = ranks.getRankIndex(name,wx.wx2g_openid);
    ctx.textAlign = "center";
    ctx.textBaseline = "middle";
    ctx.font = size + "px '" + font + "'";
    ctx.fillStyle = 'rgb(' + (color>>16) + ', ' + ((color>>8)&0xFF) + ', ' + (color&0xFF) +')';
    ctx.fillText(ranks.getAt(name, myRankIndex).value, sharedCanvas.width/2, sharedCanvas.height/2);
}

//绘制当前名次
function drawRankIndex(data) {
    let sharedCanvas = wx.getSharedCanvas();
    let ctx = sharedCanvas.getContext('2d');
    ctx.clearRect(0,0,sharedCanvas.width,sharedCanvas.height);
    let name = data.name; //排行榜名称
    let color = data.color;
    let size = data.size;
    let font = data.font;
    let ranks = wx.wx2g_ranks;
    if(!ranks.getLength(name)) return;
    ranks.sort(name);
    let myRankIndex = ranks.getRankIndex(name,wx.wx2g_openid);
    ctx.textAlign = "center";
    ctx.textBaseline = "middle";
    ctx.font = size + "px '" + font + "'";
    ctx.fillStyle = 'rgb(' + (color>>16) + ', ' + ((color>>8)&0xFF) + ', ' + (color&0xFF) +')';
    ctx.fillText(myRankIndex + 1, sharedCanvas.width>>1, sharedCanvas.height>>1);
}

//绘制下一个即将超越的好友信息
function drawNextRank(data) {
    let sharedCanvas = wx.getSharedCanvas();
    let ctx = sharedCanvas.getContext('2d');
    ctx.clearRect(0,0,sharedCanvas.width,sharedCanvas.height);
    let name = data.name; //排行榜名称
    let style = data.style; //排行榜样
    let score = data.score;
    let ranks = wx.wx2g_ranks;
    ranks.sort(name,true);
    let rank = ranks.getValueBiggerRank(name,score);
    if(!rank) {
        rank = ranks.getAt(name,ranks.getRankIndex(name,wx.wx2g_openid));
    }
    if(style == 1) {
        //绘制头像
        ctx.drawImage(rank.user.img, 200, 0, 80, 80);
        //绘制分数
        ctx.textAlign = "left";
        ctx.textBaseline = "alphabetic";
        ctx.font = "27px '微软雅黑'";
        ctx.fillStyle = 'rgb(255, 251, 235)';
        ctx.fillText(rank.value + "分", sharedCanvas.width/2, sharedCanvas.height/2 + 15,900);
    }else if(style == 1000) {
        //绘制头像
        ctx.drawImage(rank.user.img, sharedCanvas.width/2 - 45, 35, 90, 90);
        //绘制头像遮罩
        //ctx.drawImage(createImage("res/rank/mask.png"), (sharedCanvas.width - 123)/2, (sharedCanvas.height - 191)/2, 123, 191);
        //设置字体
        ctx.textAlign = "center";
        ctx.textBaseline = "alphabetic";
        ctx.font = "20px '微软雅黑'";
        ctx.fillStyle = 'rgb(154, 152, 152)';
        //即将超越
        ctx.fillText(rank.user.openid == wx.wx2g_openid ? "成功登顶" :"即将超越", sharedCanvas.width/2, 24);
        //绘制名称
        ctx.fillText(rank.user.name, sharedCanvas.width/2, 154);
        //绘制分数
        ctx.fillText(rank.value, sharedCanvas.width/2, 154 + 28);
    }else if(style == 3){
        //绘制头像
        // ctx.drawImage(rank.user.img, sharedCanvas.width/2 - 45, 35, 90, 90);
        ctx.drawImage(rank.user.img, 21, 33, 73, 73);
        // //绘制头像遮罩
       // ctx.drawImage(createImage("res/rank/nextRankBg.png"), 0, 0, 113, 170);
        // //设置字体
        ctx.textAlign = "center";
        ctx.textBaseline = "alphabetic";
        ctx.font = "16px '微软雅黑'";
        ctx.fillStyle = 'rgb(154, 152, 152)';
        // //即将超越
        ctx.fillText(rank.user.openid == wx.wx2g_openid ? "成功登顶" :"即将超越", sharedCanvas.width/2, 28);
        // //绘制名称
        ctx.fillText(rank.user.getNameLength(5), sharedCanvas.width/2, 125);
        // //绘制分数
        ctx.fillText(rank.value, sharedCanvas.width/2, 125 + 25);
    }else if(style == 4){
        //绘制头像
        // ctx.drawImage(rank.user.img, sharedCanvas.width/2 - 45, 35, 90, 90);
        ctx.drawImage(rank.user.img, 5, 20 + 22, 65, 65);
        // //绘制头像遮罩
       // ctx.drawImage(createImage("res/rank/nextRankBg2.png"), 0, 22, 168, 99);
        // //设置字体
        ctx.textAlign = "center";
        ctx.textBaseline = "alphabetic";
        ctx.font = "16px '微软雅黑'";
        ctx.fillStyle = 'rgb(255, 255, 255)';
        // //即将超越
        ctx.fillText(rank.user.openid == wx.wx2g_openid ? "成功登顶" :"即将超越", 32, 16);
        // //绘制名称
        ctx.fillText(rank.user.getNameLength(5), 120, 65);
        // //绘制分数
        ctx.fillText(rank.value, 120, 70 + 25);
    }
}

function drawOverStep(data) {
    let sharedCanvas = wx.getSharedCanvas();
    let ctx = sharedCanvas.getContext('2d');
    ctx.clearRect(0,0,sharedCanvas.width,sharedCanvas.height);
    let name = data.name; //排行榜名称
    let style = data.style; //排行榜样
    let score = data.score;
    let ranks = wx.wx2g_ranks;
    ranks.sort(name);
    let myRankIndex = ranks.getRankIndex("rank",wx.wx2g_openid);
    let allNum = ranks.ranks.rank.length;
    let per = (allNum - 1 - myRankIndex) / ranks.ranks.rank.length;


    if(data.style == 2){
        ctx.fillStyle = 'rgb(255, 255, 255)';
    }
    ctx.textAlign = "center";
    ctx.textBaseline = "alphabetic";

    ctx.font = "30px '微软雅黑'";
    ctx.fillText("已超越了" + per + "%的玩家", sharedCanvas.width/2, 40);


}
function  drawNextUser(data) {
    let sharedCanvas = wx.getSharedCanvas();
    let ctx = sharedCanvas.getContext('2d');
    ctx.clearRect(0,0,sharedCanvas.width,sharedCanvas.height);
    let name = data.name; //排行榜名称
    let style = data.style; //排行榜样
    let score = data.score;
    let ranks = wx.wx2g_ranks;
    ranks.sort(name);
    let myRankIndex = ranks.getRankIndex(name,wx.wx2g_openid);
    let myRank = ranks.getAt(name,myRankIndex);

    ctx.fillStyle = 'rgb(255, 255, 255)';
    ctx.textAlign = "center";
    ctx.textBaseline = "alphabetic";
    //绘制比自己名次高的
    let rank = ranks.getAt(name,myRankIndex - 1);
    if(rank) {
        ctx.drawImage(rank.user.img, sharedCanvas.width/2 - 60 / 2, 0, 60, 60);
        ctx.font = "19px '微软雅黑'";
        ctx.fillText("距离上一名好友还有" + (rank.value - myRank.value) + "分", sharedCanvas.width/2, 90);
    }else{
        ctx.drawImage(myRank.user.img, sharedCanvas.width/2 - 60 / 2, 0, 60, 60);
        ctx.font = "19px '微软雅黑'";
        ctx.fillText("您已登顶", sharedCanvas.width/2, 90);
    }
}
//绘制迷你排行榜
function drawMiniRank(data) {
    let sharedCanvas = wx.getSharedCanvas();
    let ctx = sharedCanvas.getContext('2d');
    ctx.clearRect(0,0,sharedCanvas.width,sharedCanvas.height);
    let name = data.name; //排行榜名称
    let style = data.style; //排行榜样
    let score = data.score;
    let ranks = wx.wx2g_ranks;
    ranks.sort(name);
    let myRankIndex = ranks.getRankIndex(name,wx.wx2g_openid);

    if (data.style == 1) {
        ctx.fillStyle = 'rgb(0, 0, 0)';
    }else if(data.style == 2){
        ctx.fillStyle = 'rgb(255, 255, 255)';
    }
    ctx.textAlign = "center";
    ctx.textBaseline = "alphabetic";
    //绘制比自己名次高的
    let rank = ranks.getAt(name,myRankIndex - 1);
    if(rank) {
        //绘制排名
        ctx.font = "19px '微软雅黑'";
        if (data.style == 1) {
            ctx.fillText(myRankIndex, sharedCanvas.width/2 - 165, 28);
        }
        //绘制头像
        if (data.style == 1) {
            ctx.drawImage(rank.user.img, sharedCanvas.width/2 - 34 - 165, 43, 68, 68);
        }else if (data.style == 2){
            ctx.drawImage(rank.user.img, sharedCanvas.width/2 - 40 - 165, 20, 80, 80);
        }
        ctx.font = "19px '微软雅黑'";
        if (data.style == 1) {
            ctx.fillText(rank.user.name, sharedCanvas.width/2 - 165, 148);
        }else if (data.style == 2){
            ctx.font = "24px '微软雅黑'";
            ctx.fillText(rank.user.getNameLength(5), sharedCanvas.width/2 - 165, 128);
        }
        if (data.style == 2) {
            ctx.font = "38px '微软雅黑'";
        }
        if (data.style == 1) {
            ctx.fillText(rank.value, sharedCanvas.width/2 - 165, 185);
        }else if (data.style == 2){
            ctx.fillText(rank.value, sharedCanvas.width/2 - 165, 175);
        }
    }

    //绘制自己的
    rank = ranks.getAt(name,myRankIndex);
    //绘制排名
    ctx.font = "33px '微软雅黑'";
    if (data.style == 1) {
        ctx.fillText(myRankIndex + 1, sharedCanvas.width/2, 28);
    }
    //绘制头像
    if (data.style == 1) {
        ctx.drawImage(rank.user.img, sharedCanvas.width/2 - 34, 43, 68, 68);
    }else if (data.style == 2){
        ctx.drawImage(rank.user.img, sharedCanvas.width/2 - 40, 20, 80, 80);
    }
    ctx.font = "24px '微软雅黑'";
    if (data.style == 1) {
        ctx.fillText(rank.user.name, sharedCanvas.width/2, 148);
    }else if (data.style == 2){
        ctx.fillText(rank.user.getNameLength(5), sharedCanvas.width/2, 128);
    }
    if (data.style == 2) {
        ctx.font = "38px '微软雅黑'";
    }
    if (data.style == 1) {
        ctx.fillText(rank.value, sharedCanvas.width/2, 185);
    }else if (data.style == 2){
        ctx.fillText(rank.value, sharedCanvas.width/2, 175);
    }

    //绘制比自己名次低的
    rank = ranks.getAt(name,myRankIndex + 1);
    if(rank) {
        //绘制排名
        ctx.font = "19px '微软雅黑'";
        if (data.style == 1) {
            ctx.fillText(myRankIndex + 2, sharedCanvas.width/2 + 165, 28);
        }
        //绘制头像
        if (data.style == 1) {
            ctx.drawImage(rank.user.img, sharedCanvas.width/2 - 34 + 165, 43, 68, 68);
        }else{
            ctx.drawImage(rank.user.img, sharedCanvas.width/2 - 40 + 165, 20, 80, 80);
        }
        ctx.font = "19px '微软雅黑'";
        if (data.style == 1) {
            ctx.fillText(rank.user.name, sharedCanvas.width/2 - 165, 148);
        }else if (data.style == 2){
            ctx.font = "24px '微软雅黑'";
            ctx.fillText(rank.user.getNameLength(5), sharedCanvas.width/2  + 165, 128);
        }
        if (data.style == 2) {
            ctx.font = "38px '微软雅黑'";
        }
        if (data.style == 1) {
            ctx.fillText(rank.value, sharedCanvas.width/2 + 165, 185);
        }else if (data.style == 2){
            ctx.fillText(rank.value, sharedCanvas.width/2 + 165, 175);
        }

    }
}

function drawHead(data) {
    let sharedCanvas = wx.getSharedCanvas();
    let ctx = sharedCanvas.getContext('2d');
    ctx.clearRect(0,0,sharedCanvas.width,sharedCanvas.height);
    let style = data.style;
    if(style == 1) {
        ctx.drawImage(wx.wx2g_ranks.user.img, 0, 0, 40, 40);
    }else if(style == 2){
        ctx.drawImage(wx.wx2g_ranks.user.img, 0, 0, 60, 60);
    }
}

function drawShowRank(data) {
    let sharedCanvas = wx.getSharedCanvas();
    let ctx = sharedCanvas.getContext('2d');
    ctx.clearRect(0,0,sharedCanvas.width,sharedCanvas.height);
    let style = data.style;
    let name = data.name;
    let ranks = wx.wx2g_ranks;
    ranks.sort(name);
    let myRankIndex = ranks.getRankIndex(name,wx.wx2g_openid);
    if(style == 1) {
        let str = (ranks.getLength(name) - myRankIndex - 1) + "";
        for(let i = 0; i < str.length; i++) {
            ctx.drawImage(createImage("res/show/" + str[i] + ".png"), 40 * i + 15, 3, 55, 65);
        }
        ctx.drawImage(createImage("res/show/bg2.png"), 40 * str.length + 15, 3, 144, 64);
        str = (myRankIndex + 1) + "";
        for(let i = 0; i < str.length; i++) {
            ctx.drawImage(createImage("res/show/" + str[i] + ".png"), 40 * i, 3 + 66, 55, 65);
        }
        ctx.drawImage(createImage("res/show/bg3.png"), 40 * str.length, 69, 58, 64 );
    }
}

function drawMiniHonor(data) {
    let sharedCanvas = wx.getSharedCanvas();
    let ctx = sharedCanvas.getContext('2d');
    ctx.clearRect(0,0,sharedCanvas.width,sharedCanvas.height);
    let style = data.style;
    let name = data.name;
    let ranks = wx.wx2g_ranks;
    ranks.sort(name);
    let myRankIndex = ranks.getRankIndex(name,wx.wx2g_openid);
    let myRank = ranks.getAt(name,myRankIndex);
    ctx.fillStyle = 'rgb(0, 0, 0)';
    ctx.textAlign = "center";
    ctx.textBaseline = "alphabetic";
    if(style == 1) {
        //绘制自己的荣誉信息
        ctx.font = "20px '微软雅黑'";
        let honor = getHonor(myRank.value);
        ctx.drawImage(createImage(honor.bg1), sharedCanvas.width/2 - 98  * 0.5 + 0, -3,123 ,44);
        ctx.fillText(honor.name, sharedCanvas.width/2 + 10, 26);
        ctx.drawImage(myRank.user.img, sharedCanvas.width * 0.5 - 36, 28 + 10, 100, 100);
        ctx.drawImage(createImage(honor.icon), sharedCanvas.width * 0.5 - 35 + honor.iconx * 0.9, 28 + honor.icony + 25, 84 * 0.9, 92 * 0.9);
        ctx.font = "60px '微软雅黑'";
        ctx.fillStyle = 'rgb(0, 0, 0)';
        let moveY = 30;
        //绘制最高积分和最高排名
        ctx.fillText(myRank.value, sharedCanvas.width/2 - 124 - 20, 143 + 34 + 17 + moveY);
        ctx.fillText(myRankIndex + 1, sharedCanvas.width/2 - 124 + 240 + 37, 143 + 34 + 17 + moveY);
        //绘制低于自己分的好友
        let list = [myRankIndex + 1,myRankIndex + 2, myRankIndex + 3];
        let rank;
        for(let i = 0; i < list.length; i++) {
            if(list[i] >= ranks.getLength(name)) {
                list.splice(i,1);
                i--;
            }
        }
        moveY += -40;
        for(let i = 0; i < list.length; i++) {
            ctx.font = "17px '微软雅黑'";
            ctx.fillStyle = 'rgb(0, 0, 0)';
            rank = ranks.getAt(name, list[i]);
            honor = getHonor(rank.value);
            let moveX = 175 * i + 100 - (list.length - 1) * 175 * 0.5;
            ctx.drawImage(createImage(honor.bg), sharedCanvas.width * 0.5 - 130 - 36 - 10 + moveX - 5 - 3, 218 + 55 + 50 + 15 + moveY,173 ,247);
            ctx.drawImage(rank.user.img, sharedCanvas.width * 0.5 - 130 - 36 + moveX + 45 - 5, 218 + 55 + moveY + 3 + 180, 65, 65);
            ctx.fillText(honor.name, sharedCanvas.width * 0.5 - 130 - 10 + moveX + 7 + 47 - 10, 218 + 55 + 50 + 20 + 3 + moveY + 15 + 78 - 6);
            ctx.fillText(rank.user.name, sharedCanvas.width * 0.5 - 130 - 3 + moveX + 40, 218 + 55 + 50 + 18 + 18 + moveY + 72 + 121);
            ctx.drawImage(createImage(honor.icon), sharedCanvas.width * 0.5 - 130 - 36 + moveX + honor.iconx * 6/8 + 68, 218 + 55 + 30 + honor.icony * 6/8 + moveY + 15, 84 * 6/8, 92 * 6/8);
        }
    }
}

function getHonor(score) {
    if(score <= 100) return {name:"新手",icon:"res/show/1.png",iconx:-40,icony:10,bg:"res/show/honorbg1.png",bg1:"res/show/honorbg1_1.png"};
    if(score <= 300) return {name:"高手",icon:"res/show/2.png",iconx:-40,icony:22,bg:"res/show/honorbg2.png",bg1:"res/show/honorbg1_2.png"};
    if(score <= 500) return {name:"大师",icon:"res/show/3.png",iconx: - 40,icony:32,bg:"res/show/honorbg3.png",bg1:"res/show/honorbg1_3.png"};
    if(score <= 1000) return {name:"神仙",icon:"res/show/4.png",iconx:-40,icony:35,bg:"res/show/honorbg4.png",bg1:"res/show/honorbg1_4.png"};
    return {name:"超神霸主",icon:"res/show/5.png",iconx:0,icony:-20,bg:"res/show/honorbg5.png",bg1:"res/show/honorbg1_5.png"};
}

//重置荣誉殿堂页数
function resetHonorPage(data) {
    wx.wx2g_honorsShow[data.name] = {page:0,pageCount:data.pageCount};
}

//荣誉殿堂向左翻页
function honorPageLeft(data) {
    if(wx.wx2g_honorsShow[data.name].page > 0) wx.wx2g_honorsShow[data.name].page--;
}

//荣誉殿堂向右翻页
function honorPageRight(data) {
    let ranks = wx.wx2g_ranks;
    ranks.sort(data.name);
    let len = Math.ceil((wx.wx2g_ranks.getLength(data.name) - ranks.getRankIndex(data.name,wx.wx2g_openid) - 1) / wx.wx2g_honorsShow[data.name].pageCount);
    if(wx.wx2g_honorsShow[data.name].page < len - 1 )
        wx.wx2g_honorsShow[data.name].page++;
}

//绘制荣誉殿堂
function drawHonor(data) {
    let name = data.name; //排行榜名称
    let style = data.style; //排行榜样
    updateHonorShow(name,style,wx.wx2g_honorsShow[name].page,wx.wx2g_honorsShow[name].pageCount);
}

function updateHonorShow(name,style,page,pageCount) {
    let sharedCanvas = wx.getSharedCanvas();
    let ctx = sharedCanvas.getContext('2d');
    ctx.clearRect(0,0,sharedCanvas.width,sharedCanvas.height);
    let ranks = wx.wx2g_ranks;
    ranks.sort(name);
    let myRankIndex = ranks.getRankIndex(name,wx.wx2g_openid);
    let myRank = ranks.getAt(name,myRankIndex);
    if(style == 1) {
        let len = Math.ceil((wx.wx2g_ranks.getLength(name) - ranks.getRankIndex(name,wx.wx2g_openid) - 1) / pageCount);
        ctx.textBaseline = "alphabetic";
        //绘制页面数字
        ctx.textAlign = "center";
        ctx.font = "28px '微软雅黑'";
        ctx.fillStyle = 'rgb(0, 0, 0)';
        ctx.fillText((page + 1) + "/" + (len || 1), 275 + 15, 45 + 115 * 5 + 270);

        //绘制自己的荣誉信息
        ctx.font = "20px '微软雅黑'";
        ctx.fillStyle = 'rgb(0, 0, 0)';
        let honor = getHonor(myRank.value);
        ctx.drawImage(createImage(honor.bg1), sharedCanvas.width/2 - 98  * 0.5 + 5 - 5, -3 + 20 + 5,98 * 1.3 ,35 * 1.3);
        ctx.fillText(honor.name, sharedCanvas.width/2 + 15, 19 + 33);
        ctx.drawImage(myRank.user.img, sharedCanvas.width * 0.5 - 35, 28 + 40, 100, 100);
        ctx.drawImage(createImage(honor.icon), sharedCanvas.width * 0.5 - 35 + honor.iconx, 28 + 40 + honor.icony * 1.5, 84, 92);

        //绘制最高分数和排名
        ctx.font = "58px '微软雅黑'";
        ctx.fillStyle = 'rgb(0, 0, 0)';
        ctx.fillText(myRank.value, sharedCanvas.width/2 - 124 - 25, 143 + 34 + 75);
        ctx.fillText(myRankIndex + 1, sharedCanvas.width/2 - 124 + 240 + 40, 143 + 34 + 75);

        let list = [];
        for(let i = 0; i < pageCount && page * pageCount + myRankIndex + 1 + i  < ranks.getLength(name); i++) {
            list.push(page * pageCount + myRankIndex + 1 + i);
        }
        let rank;
        for(let i = 0; i < list.length; i++) {
            if(list[i] < ranks.getLength(name)) {
                ctx.font = "16px '微软雅黑'";
                ctx.fillStyle = 'rgb(0, 0, 0)';
                rank = ranks.getAt(name, list[i]);
                honor = getHonor(rank.value);
                let moveX = 175 * (i%3) - 45 - 27;
                let moveY = 0 + 262 * (~~(i/3)) + 15;
                ctx.drawImage(createImage(honor.bg), sharedCanvas.width * 0.5 - 130 - 36 - 15 + moveX, 218 + 55 + 63 + moveY,173,247);
                ctx.drawImage(rank.user.img, sharedCanvas.width * 0.5 - 130 - 36 + moveX + 36, 218 + 55 + moveY + 182, 70, 70);
                ctx.fillText(honor.name, sharedCanvas.width * 0.5 - 130 - 3 + moveX + 40, 218 + 55 + 50 + 18 + 18 + moveY + 72);
                ctx.fillText(rank.user.name, sharedCanvas.width * 0.5 - 130 - 3 + moveX + 40, 218 + 55 + 50 + 18 + 18 + moveY + 72 + 121);
                ctx.drawImage(createImage(honor.icon), sharedCanvas.width * 0.5 - 130 - 36 + moveX + honor.iconx * 6 / 8 + 73, 218 + 55 + honor.icony * 6/8 + moveY + 44, 84 * 6/8, 92 * 6/8);
            }
        }
    }
}

function drawBg(color = 0) {
    let sharedCanvas = wx.getSharedCanvas();
    let ctx = sharedCanvas.getContext('2d');
    ctx.fillStyle = 'rgb('+(color>>16)+', '+((color>>8)&0xff)+', '+(color&0xff)+')';
    ctx.beginPath();
    ctx.fillRect(0,0,sharedCanvas.width,sharedCanvas.height);
    ctx.closePath();
}